using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;

namespace Matrix.Notification.EntityFrameworkCore
{
    /// <summary>
    /// NotificationDbContext扩展方法
    /// </summary>
    public static class NotificationDbContextExtensions
    {
        /// <summary>
        /// 配置通知模块数据库
        /// </summary>
        public static IServiceCollection ConfigureNotificationDbContext(
            this IServiceCollection services,
            Action<AbpDbContextConfigurationOptions>? configureOptions = null)
        {
            services.AddAbpDbContext<NotificationDbContext>(options =>
            {
                options.AddDefaultRepositories(includeAllEntities: true);
                configureOptions?.Invoke(options);
            });

            return services;
        }

        /// <summary>
        /// 配置通知模块数据库使用PostgreSQL
        /// </summary>
        public static IServiceCollection ConfigureNotificationPostgreSql(
            this IServiceCollection services,
            string connectionString,
            bool useLazyLoading = false)
        {
            services.AddAbpDbContext<NotificationDbContext>(options =>
            {
                options.UseNpgsql(connectionString);
                options.AddDefaultRepositories(includeAllEntities: true);

                if (useLazyLoading)
                {
                    options.UseLazyLoadingProxies();
                }
            });

            return services;
        }

        /// <summary>
        /// 配置通知模块数据库使用内存数据库（用于测试）
        /// </summary>
        public static IServiceCollection ConfigureNotificationInMemory(
            this IServiceCollection services,
            string databaseName = "NotificationTestDb")
        {
            services.AddAbpDbContext<NotificationDbContext>(options =>
            {
                options.UseInMemory(databaseName);
                options.AddDefaultRepositories(includeAllEntities: true);
            });

            return services;
        }

        /// <summary>
        /// 添加通知模块数据库迁移
        /// </summary>
        public static async Task MigrateNotificationDatabaseAsync(
            this IServiceProvider serviceProvider,
            CancellationToken cancellationToken = default)
        {
            var dbContext = serviceProvider.GetRequiredService<NotificationDbContext>();
            await dbContext.Database.MigrateAsync(cancellationToken);
        }

        /// <summary>
        /// 确保通知模块数据库已创建
        /// </summary>
        public static async Task EnsureNotificationDatabaseCreatedAsync(
            this IServiceProvider serviceProvider,
            CancellationToken cancellationToken = default)
        {
            var dbContext = serviceProvider.GetRequiredService<NotificationDbContext>();
            await dbContext.Database.EnsureCreatedAsync(cancellationToken);
        }

        /// <summary>
        /// 删除通知模块数据库
        /// </summary>
        public static async Task DropNotificationDatabaseAsync(
            this IServiceProvider serviceProvider,
            CancellationToken cancellationToken = default)
        {
            var dbContext = serviceProvider.GetRequiredService<NotificationDbContext>();
            await dbContext.Database.EnsureDeletedAsync(cancellationToken);
        }
    }
}